package org.tbjd.client.listeners.projectSwitcher;

import java.util.HashMap;
import java.util.Map;

import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.constants.ProjectsGridConstants;
import org.tbjd.client.exceptions.NoUpperDecisionmakerFoundException;
import org.tbjd.client.exceptions.TBJDException;
import org.tbjd.client.helpers.ProjectStatusFilterHelper;
import org.tbjd.client.interfaces.Decisionmaker;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;

public class StatusFiltersListener implements ClickListener, Decisionmaker {

	private Widget sender = null;

	public void onClick(Widget sender) {
		// store the sender because the map will erase it
		this.sender = sender;
		System.out.println("StatusFiltersListener.onClick(" + sender.getTitle() + ")");

		int status = Integer.parseInt(sender.getTitle());
		switch (status) {
		case ProjectsGridConstants.RUNNING:
			ProjectStatusFilterHelper.getInstance().setRunning(!ProjectStatusFilterHelper.getInstance().isRunning());
			break;

		case ProjectsGridConstants.PAUSED:
			ProjectStatusFilterHelper.getInstance().setPaused(!ProjectStatusFilterHelper.getInstance().isPaused());
			break;

		case ProjectsGridConstants.STOPPED:
			ProjectStatusFilterHelper.getInstance().setStopped(!ProjectStatusFilterHelper.getInstance().isStopped());
			break;

		case ProjectsGridConstants.FINISHED:
			ProjectStatusFilterHelper.getInstance().setFinished(!ProjectStatusFilterHelper.getInstance().isFinished());
			break;

		case ProjectsGridConstants.FAVORITES:
			ProjectStatusFilterHelper.getInstance().setFavorites(!ProjectStatusFilterHelper.getInstance().isFavorites());
			break;

		case ProjectsGridConstants.PRIORITY:
			ProjectStatusFilterHelper.getInstance().setPriority(!ProjectStatusFilterHelper.getInstance().isPriority());
			break;

		case ProjectsGridConstants.SEARCH:
			ProjectStatusFilterHelper.getInstance().setSearch(!ProjectStatusFilterHelper.getInstance().isSearch());
			break;
		}

		Map<Integer, Object> map = new HashMap<Integer, Object>();
		map.put(NotificationsConstants.FETCH_PROJECTS, ProjectStatusFilterHelper.getInstance());
		decide(map);
	}

	public void decide(Object action) {
		System.out.println("StatusFiltersListener.decide(" + action + ")");

		try {
			delegate(action);
		} catch (NoUpperDecisionmakerFoundException e) {
			e.printStackTrace();
		}
	}

	public void delegate(Object action) throws NoUpperDecisionmakerFoundException {
		System.out.println("StatusFiltersListener.delegate(" + action + ")");

		// passes info to the first responsible
		Widget parent = this.sender.getParent();
		while (parent != null && parent instanceof Decisionmaker == false) {
			parent = ((Widget) parent).getParent();
		}
		if (parent != null) {
			try {
				((Decisionmaker) parent).decide(action);
			} catch (TBJDException e) {
				e.printStackTrace();
			}
		} else {
			throw new NoUpperDecisionmakerFoundException();
		}
	}

	public void handle(Object action) {
	}

}
